import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:thinking/config/_.dart';
import 'package:thinking/entity/_.dart';
import 'package:thinking/service/_.dart';
import 'package:thinking/utils/SharedPreferences.dart';

class FollowButton extends StatefulWidget{

  final UserData userData;

  FollowButton({required this.userData});

  @override
  _FollowButton createState() => _FollowButton(userData: userData);

}


class _FollowButton extends State<FollowButton> with SingleTickerProviderStateMixin{


  UserData userData;
  late AnimationController _animationController;
  late Animation<double> _animation;

  _FollowButton({required this.userData});

  @override
  void initState() {
    super.initState();
    _animationController = AnimationController(vsync: this, duration: Duration(milliseconds: 300));
    _animation = Tween(begin: 0.0, end: 1.0).animate(_animationController);
    _animationController.forward();
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 80,
      child: FadeTransition(
          opacity: _animation,
          child: ElevatedButton(
            style: ButtonStyle(
              textStyle: MaterialStateProperty.all(TextStyle(fontSize: 14)),
              backgroundColor:MaterialStateProperty.all(userData.isFollow!?ColorConfig.LineColor:ColorConfig.ThemeColor),
              shape:MaterialStateProperty.all(
                  RoundedRectangleBorder(
                      borderRadius: BorderRadius.all(Radius.circular(8))
                  )
              ),
              foregroundColor:
              MaterialStateProperty.all(userData.isFollow!?ColorConfig.TextColor:ColorConfig.WhiteBackColor),

            ),
            child: userData.isFollow!?Text(userData.userFans!<=999?userData.userFans!.toString()+' 粉丝':userData.userFans!>=10000?'${userData.userFans!~/10000}w+ 粉丝':'${userData.userFans!~/1000}k+ 粉丝',):
            Row(children: [Icon(Icons.add,size: 13,),SizedBox(width: 5,),Text('关注')],),
            onPressed: () async{
              if(_animation.status == AnimationStatus.forward || _animation.status == AnimationStatus.reverse) return;
              userData.setIsFollow = !userData.isFollow!;
              await UserService.changeUserFollow(params:{
                "user_Id": SPreferences().getInt('user_Id'),
                "follow_Id":userData.userId,
                "isFollow":userData.isFollow
              });
              setState(() {
                if(userData.isFollow!){
                  userData.setUserFans = userData.userFans! +1;
                  BotToast.showText(text: '关注成功~');
                }else{
                  userData.setUserFans = userData.userFans! -1;
                }
              });
            },
          )
      ),
    );
  }

}